package cn.itsource.crm.web.controller;

import cn.itsource.AjaxResult;
import cn.itsource.crm.domain.DictInfo;
import cn.itsource.crm.service.IDictInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/dictInfo")
@Api(tags = "数据字典明细类")
@CrossOrigin
public class DictInfoController {

    @Autowired
    private IDictInfoService dictInfoService;

    @RequestMapping(value = "/queryAll",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="查询字典所有明细",notes = "queryAll查询方法")
    @CrossOrigin
    public List<DictInfo> queryAll(){
        System.out.println("patch请求");
        return dictInfoService.queryAll();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value="根据ID查询类型",notes = "queryOne查询方法")
    @ResponseBody
    public DictInfo getOne(@PathVariable Long id){
        return  dictInfoService.queryOne(id);
    }

     @RequestMapping(value = "/queryByType",method = RequestMethod.PATCH)
     @ResponseBody
     @ApiResponses({
             @ApiResponse(code=200,message="成功"),
             @ApiResponse(code=500,message="服务器错误")
     })
     @ApiOperation(value="根据字典的type查询明细",notes = "queryByType查询方法")
     @CrossOrigin
     public List<DictInfo> qyeryByType(@PathVariable String type){
         System.out.println("patch请求");
         return dictInfoService.queryByType(type);
     }


    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value="保存字典明细的方法",notes = "save方法")
    public AjaxResult save(@RequestBody  DictInfo dictInfo){
        try {
            System.out.println("接收的dictInfo对象:"+dictInfo);
            dictInfoService.save(dictInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");
        }
        return new AjaxResult();
    }
    //删除
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value="根据id删除字典类型方法",notes = "delete方法")
    public AjaxResult delete(@PathVariable Long id){
        try {
            System.out.println("删除的id是:"+id);
            dictInfoService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");

        }
        return new AjaxResult();
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改字典类型方法",notes = "update方法")
    public AjaxResult update(@RequestBody  DictInfo dictInfo){
        try {
            System.out.println("修的对象:"+dictInfo);
            System.out.println(dictInfo.getId());
            dictInfoService.update(dictInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }
}
